package com.hserver.docker.controller;

import com.hserver.docker.config.ConstConfig;
import com.hserver.docker.service.DockerService;
import top.hserver.core.interfaces.HttpRequest;
import top.hserver.core.interfaces.HttpResponse;
import top.hserver.core.ioc.annotation.Autowired;
import top.hserver.core.ioc.annotation.Controller;
import top.hserver.core.ioc.annotation.GET;
import top.hserver.core.server.util.JsonResult;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yiChenLove
 */
@Controller("/docker/")
public class DockerController {

    @Autowired
    private DockerService dockerService;

    /**
     * 镜像管理页面
     *
     * @param response
     */
    @GET("imagesList")
    public void imagesList(HttpResponse response) {
        response.sendTemplate("docker/imagesList.ftl");
    }


    @GET("getImagesList")
    public JsonResult getImagesList() {
        return JsonResult.ok().put("data", dockerService.imagesList());
    }

    /**
     * 容器管理页面
     *
     * @param response
     */
    @GET("containerList")
    public void containerList(HttpResponse response) {
        response.sendTemplate("docker/containerList.ftl");
    }

    /**
     * 容器列表
     *
     * @return
     */
    @GET("getContainerList")
    public JsonResult getContainerList() {
        return JsonResult.ok().put("data", dockerService.containerList());
    }

    /**
     * 容器终端
     *
     * @param response
     */
    @GET("terminal/{uuid}")
    public void terminal(HttpResponse response, HttpRequest request) {
        Map<String, Object> data = new HashMap<>(1);
        data.put("uuid", request.query("uuid"));
        response.sendTemplate("docker/terminal.ftl", data);
    }


    @GET("imageDel/{id}")
    public void imageDel(HttpResponse response, String id) {
        String error = null;
        if (id != null) {
            try {
                dockerService.removeImageId(id);
            } catch (Exception e) {
                error = e.getMessage();
            }
        }
        Map<String, Object> data = new HashMap<>(2);
        data.put("images", dockerService.imagesList());
        data.put("error", error);
        response.sendTemplate("docker/imagesList.ftl", data);
    }


    @GET("containerStart/{id}")
    public void containerStart(HttpResponse response, String id) {
        String error = null;
        if (id != null) {
            try {
                dockerService.runContainerId(id);
            } catch (Exception e) {
                error = e.getMessage();
            }
        }
        Map<String, Object> data = new HashMap<>(2);
        data.put("container", dockerService.containerList());
        data.put("error", error);
        response.sendTemplate("docker/containerList.ftl", data);
    }


    @GET("containerStop/{id}")
    public void containerStop(HttpResponse response, String id) {
        String error = null;
        if (id != null) {
            try {
                dockerService.stopContainerId(id);
            } catch (Exception e) {
                error = e.getMessage();
            }
        }
        Map<String, Object> data = new HashMap<>(2);
        data.put("container", dockerService.containerList());
        data.put("error", error);
        response.sendTemplate("docker/containerList.ftl", data);
    }


    @GET("containerDel/{id}")
    public void containerDel(HttpResponse response, String id) {
        String error = null;
        if (id != null) {
            try {
                dockerService.removeContainerId(id);
            } catch (Exception e) {
                error = e.getMessage();
            }
        }
        Map<String, Object> data = new HashMap<>(2);
        data.put("container", dockerService.containerList());
        data.put("error", error);
        response.sendTemplate("docker/containerList.ftl", data);
    }

}
